<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8" />
    <title>微信模板实例化</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="/static/basic/plugins/easyui/themes/bootstrap/easyui.css">
	<link href="https://cdn.bootcss.com/font-awesome/5.11.2/css/all.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="/static/m.css" >
	
</head>
<body>
<div id="window-mmask" class="window-mmask" ><div class="window-mmask-msg" >页面加载中,请稍候...</div></div>
<div class="easyui-layout" data-options="fit:true"  >
	<div data-options="region:'center'" >
		<table id="gridList" ></table>
	</div>
	<div data-options="region:'east',collapsible:false,split:true,title:'模板数据定义'" style="width:40%;">
		<div class="easyui-layout" data-options="fit:true" style="">
			<table id="grid2" ></table>
		</div>
	</div>
</div>
<div id="searchbar">
<div class="maction">
		<a href="javascript:void(0)" id="btn1" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-plus mbutton'">新增</a>
		<a href="javascript:void(0)" id="btn2" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-pencil-alt mbutton'">编辑</a>
		<a href="javascript:void(0)" id="btn3" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-trash-alt mbutton color-c5'">删除</a>
		<!-- <a href="javascript:void(0)" id="saveButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-save mbutton'">保存</a> -->
</div>
<form id="searchForm" >
	编号<input id="search-id" name="id" class="easyui-textbox" >
	标题<input id="search-title" name="title" class="easyui-textbox" >
	<a id="searchButton" href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-search mbutton'" >查询</a>
</form>
</div>
<div id="searchbar2">
<div class="maction">
		<a href="javascript:void(0)" id="btn4" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-plus mbutton'">新增</a>
		<a href="javascript:void(0)" id="btn5" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-pencil-alt mbutton'">编辑</a>
		<a href="javascript:void(0)" id="btn6" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-trash-alt mbutton color-c5'">删除</a>
		<!-- <a href="javascript:void(0)" id="saveButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-save mbutton'">保存</a> -->
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="/static/basic/plugins/easyui/jquery.easyui.min.js"></script>
<script src="/static/init.js"></script>
<script src="/static/basic/plugins/easyui/locale/easyui-lang-zh_CN.js"></script>	
<script src="/static/searchEdit.js?v=12"></script>
<script src="/static/enum.js"></script>
<!-- <script src="https://cdn.staticfile.org/moment.js/2.24.0/moment.min.js"></script> -->
<script>
var template_instance_id;//实例id
var editRow=null;
//document ready
$(function(){
	grid1=new uniontrip.datagrid.searchEdit({
		page_id:"18481342698225669",
		grid_id:"gridList",
		sqlname:"grid_wx_msg_template_instance",
		showCheckbox:false,
		rownumbers:false,
		onClickRow:function(rowIndex, rowData){
			if(rowData.id!=template_instance_id){
				template_instance_id=rowData.id;
				grid2.loadParam({template_instance_id:template_instance_id});
			}
		},
		column_formatter:{
			content:function(value,row,index){
				//return value;
				return "<pre style='overflow:hidden'>"+value+"</pre>";
			},
			example:function(value,row,index){
				//return value;
				return "<pre style='overflow:hidden'>"+value+"</pre>";
			},
		},
		searchParamOperator:{
			title:"contain"
		},
	});
	grid2=new uniontrip.datagrid.searchEdit({
		page_id:"18481342698225670",
		grid_id:"grid2",
		sqlname:"grid_wx_msg_template_data",
		showCheckbox:false,
		rownumbers:false,
		paging:"no",
		autoload:false,
		toolbar_id:"searchbar2",
		gridParam:{"order":"asc","sort":"key"},
		column_formatter:{
			value:function(value,row,index){
				//return value;
				return "<pre style='overflow:hidden'>"+value+"</pre>";
			},
		},
		searchParamOperator:{
		},
	});
	//tab
	$('#searchButton').bind('click',grid1.load);//查询操作绑定事件
	//row
	//$('#addButton').bind('click', addRow);//新增行绑定事件
	//$('#editButton').bind('click',grid1.editRow);//编辑按钮绑定事件
	//$('#removeButton').bind('click',grid1.remove);//删除行绑定事件
	//$('#saveButton').bind('click', grid1.accept);//保存datagrid绑定事件
	//$('#cancelEditButton').bind('click',grid1.cancelEditing);//取消行编辑绑定事件
	//$('#endEditingButton').bind('click', grid1.endEditing);//结束行编辑绑定事件
	//$('#rejectButton').bind('click', grid1.rejectChanges);//撤销编辑绑定事件		
    //$('#showChangesButton').bind('click',grid1.showChanges);//信息变更提示

    //初始化搜索框
    initsearch();
    $('#btn2').bind('click',function(){
    	saveUrl="/authenticated/wx/msg/templateInstance/update";
		gridSelectRow(grid1.grid,function(row){
			editRow=row;
			EditDialogModifyMchId.dialog('setTitle','修改微信模板实例').dialog('open');
		});
	});
    $('#btn1').bind('click', function(){
    	editRow=null;
    	saveUrl="/authenticated/wx/msg/templateInstance/add";
    	EditDialogModifyMchId.dialog('setTitle','新增微信模板实例').dialog('open');
		
	});
    $('#btn3').bind('click',onDeleteRow2);//删除行绑定事件
    EditDialogModifyMchId=$("<div/>").appendTo("body").dialog({
		title:"微信模板实例",
		cls:"m-dialog",
		width:'400px',
		height:windowHeight*0.8,
		href:'wx_msg_template_instance-edit.html',
		modal:true,
		closed:true,
		//maximized:true,
		onClose:function(){
			console.log('onClose');
			
		},
		onBeforeClose:function(){//
			console.log('onBeforeClose');
			$("#form1").form('clear');
			//$("#smartwizard").data("smartWizard").options.anchorSettings.enableAllAnchors=false;
			//$('#smartwizard').smartWizard('reset');
		},
		 buttons: [{
			text:'保存',
			iconCls:'fas fa-save mbutton',
			handler:save
		},{
			text:'取消',
			iconCls:'fas fa-times mbutton',
			handler:function(){
				EditDialogModifyMchId.dialog('close')
			}
		}],
		onBeforeOpen:function(){
			console.log('onBeforeOpen');
			if($(this).data("panel").isLoaded){
				
			}
		},
		onOpened:function(){
			console.log('onOpened');
			if(editRow){
				$("#form1").form('load',editRow);
				$("#id").textbox('readonly',true);
				$('#d1').show();
				$('#d2').show();
			}else{
				$('#id').textbox('readonly',false);
				$('#d1').hide();
				$('#d2').hide();
			}
			
		},
		onOpen:function(){//在onLoad之前执行
			console.log('onOpen');
			console.log($(this).panel('options'));
			//console.log();
			if($(this).data("panel").isLoaded){
				$(this).panel('options').onOpened();
			}
		},
		onBeforeLoad:function(){
			console.log('onBeforeLoad');
		},
		onLoad:function(){//只加载一次
			console.log('onLoad');
			//document.getElementById("id").fireEvent("click");
			$(this).panel('options').onOpened();
			//$('#lng').iCheck();
		},
	});
    $('#btn5').bind('click',function(){
    	saveUrl2="/authenticated/wx/msg/templateData/update";
		gridSelectRow(grid2.grid,function(row){
			editRow2=row;
			dialog2.dialog('setTitle','修改数据').dialog('open');
		});
	});
    $('#btn4').bind('click', function(){
    	gridSelectRow(grid1.grid,function(row){
    		editRow2=null;
        	saveUrl2="/authenticated/wx/msg/templateData/add";
        	dialog2.dialog('setTitle','新增数据').dialog('open');
		});
    	
		
	});
    $('#btn6').bind('click',function (){
    	gridConfirm(grid2.grid,"是否确认删除",function (row){
    		$.post("/authenticated/wx/msg/templateData/delete",{"id":row.id},function(result){
    			if(result.success){
    				info("删除成功！");
    				grid2.loadParam({template_instance_id:template_instance_id});
    			}else{
    				$.messager.alert("提示", "删除失败！<br>"+result.message);
    			}
    		});
    	});
    });//删除行绑定事件
    dialog2=$("<div/>").appendTo("body").dialog({
		title:"微信模板实例数据",
		cls:"m-dialog",
		width:'400px',
		height:windowHeight*0.8,
		href:'wx_msg_template_data-edit.html',
		modal:true,
		closed:true,
		//maximized:true,
		onClose:function(){
			console.log('onClose');
			
		},
		onBeforeClose:function(){//
			console.log('onBeforeClose');
			$("#form2").form('clear');
			//$("#smartwizard").data("smartWizard").options.anchorSettings.enableAllAnchors=false;
			//$('#smartwizard').smartWizard('reset');
		},
		 buttons: [{
			text:'保存',
			iconCls:'fas fa-save mbutton',
			handler:save2
		},{
			text:'取消',
			iconCls:'fas fa-times mbutton',
			handler:function(){
				dialog2.dialog('close')
			}
		}],
		onBeforeOpen:function(){
			console.log('onBeforeOpen');
			if($(this).data("panel").isLoaded){
				
			}
		},
		onOpened:function(){
			console.log('onOpened');
			if(editRow2){
				$("#form2").form('load',editRow2);
				$("#key").textbox('readonly',true);
			}else{
				$('#key').textbox('readonly',false);
				$("#color").textbox('setValue','#173177');
			}
			
		},
		onOpen:function(){//在onLoad之前执行
			console.log('onOpen');
			console.log($(this).panel('options'));
			//console.log();
			if($(this).data("panel").isLoaded){
				$(this).panel('options').onOpened();
			}
		},
		onBeforeLoad:function(){
			console.log('onBeforeLoad');
		},
		onLoad:function(){//只加载一次
			console.log('onLoad');
			//document.getElementById("id").fireEvent("click");
			$(this).panel('options').onOpened();
			//$('#lng').iCheck();
		},
	});
});

function onDeleteRow2(){
	gridConfirm(grid1.grid,"是否确认删除",function (row){
		$.post("/authenticated/wx/msg/templateInstance/delete",{"id":row.id},function(result){
			if(result.success){
				info("删除成功！");
				grid1.load();
			}else{
				$.messager.alert("提示", "删除失败！<br>"+result.message);
			}
		});
	});
}
function save2(){
	if($("#form2").form('enableValidation').form('validate')){
		let post=$("#form2").serializeObject();
		post.template_instance_id=template_instance_id;
		$.post(saveUrl2,post,function(result){
			if(result.success){
				info( "保存成功！");
				dialog2.dialog('close');
				grid2.loadParam({template_instance_id:template_instance_id});
			}else{
				$.messager.alert("提示", "保存失败！<br>"+result.message);
			}
		});
	}
}
function save(){
	//if($("#form1").form('enableValidation').form('validate')){
		$.post(saveUrl,$("#form1").serializeObject(),function(result){
			if(result.success){
				info( "保存成功！");
				EditDialogModifyMchId.dialog('close');
				grid1.load();
			}else{
				$.messager.alert("提示", "保存失败！<br>"+result.message);
			}
		});
	//}
}
/**
 * 新增一行
 */
function addRow(){
//ROW
	var newRowData={id:guid()};//初始化行数据,creator_id:sessionuser.id,creator_name:sessionuser.nickname,create_time:moment().format('YYYY-MM-DD')
	grid1.grid.datagrid('append',newRowData);	
	//tab
	parent.addTab("新增","/view/sys/wx/msg_templateform.html");
}
function onUpdate(){
	var selectRows=grid1.grid.datagrid('getSelections');
	if(selectRows.length>0){
		parent.addTab("编辑["+selectRows[0].id+"]","/view/sys/wx/msg_templateform.html?id="+selectRows[0].id);
	}else{
		$.messager.alert("提示", "请选择一条数据！");
	}
}
/**
 * 删除提交
 * @param selectRow
 */
function deleteSubmit(selectRow){
	$.post("/authenticated/ajax/update/grid_wx_msg_template-delete",JSON.stringify({ids:[selectRow.id]}),function(result){
		if(result.status==200){
			$.messager.alert("提示", "删除成功！");
			grid1.load();
		}
	});
}

function onDeleteRow(){
	gridConfirm(grid1.grid,"是否确认删除",deleteRow);
}
function deleteRow(row){
	$.post("/authenticated/xx/delete",JSON.stringify({"id":row.id}),function(result){
		if(result.status==200){
			$.messager.alert("提示", "删除成功！");
			grid1.load();
		}else{
			$.messager.alert("提示", "删除失败！<br>"+result.message);
		}
	});
}


/**
 * 初始化搜索条件
 */
function initsearch(){
	/*$('#search-product_class_id').combotree({
		panelHeight:'auto',
		icons: [{
			iconCls:'icon-clear',
			handler:function(){
				$('#search-product_class_id').combotree('clear');
			}
		}],
	    data:grid1.combotreeDatas.product_class_id
	});
	//初始化combobox例子
	$('#search-status').combobox({
		panelHeight:'auto',
		editable:false,
		valueField:'code_value',
		textField:'code_name',
		icons: [{
			iconCls:'icon-clear',
			handler:function(e){
				$(e.data.target).combobox('clear');
			}
		}],
	    data:grid1.comboboxDatas.status
	});*/
}
</script>
</body>
</html>